package interfases;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import controladores.ControladorPerfil;

public class IAltaPerfil extends JFrame {
 
	private static final long serialVersionUID = 1L;
	
	private JPanel caja;
	private JTextField campoNick;
	private JTextField campoEmail;
	private JTextField titulo;
	private JButton aceptar;
	private JButton cancelar;
	private JLabel ingresoEmail;
	private JLabel ingresoNick;

/********************************************************/	
	
	public IAltaPerfil() {
		setTitle("Alta de 	Perfil");
		centrarPantalla(this, 500, 315);
		caja = new JPanel();
		caja.setBackground(new Color(255, 222, 173));
		caja.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(caja);
		caja.setLayout(null);
	
		ingresoNick = new JLabel("Ingrese nick");
		ingresoNick.setHorizontalAlignment(SwingConstants.LEFT);
		ingresoNick.setBounds(40, 101, 87, 17);
		ingresoNick.setFont(new Font("Eras Medium ITC", Font.PLAIN, 14));
		caja.add(ingresoNick);
		
		campoNick = new JTextField();
		campoNick.setBounds(137, 100, 274, 20);
		caja.add(campoNick);
		campoNick.setColumns(10);
		
		ingresoEmail = new JLabel("Ingrese email");
		ingresoEmail.setHorizontalAlignment(SwingConstants.LEFT);
		ingresoEmail.setBounds(40, 155, 87, 17);
		ingresoEmail.setFont(new Font("Eras Medium ITC", Font.PLAIN, 14));
		caja.add(ingresoEmail);
		
		campoEmail = new JTextField();
		campoEmail.setBounds(137, 154, 274, 20);
		caja.add(campoEmail);
		campoEmail.setColumns(10);
		
		aceptar = new JButton("Aceptar");
		aceptar.setBounds(215, 242, 116, 23);
		aceptar.setFont(new Font("Eras Medium ITC", Font.PLAIN, 15));
		caja.add(aceptar);
		
		titulo = new JTextField();
		titulo.setEnabled(false);
		titulo.setBackground(new Color(128, 0, 0));
		titulo.setFont(new Font("Eras Medium ITC", Font.BOLD, 20));
		titulo.setForeground(Color.WHITE);
		titulo.setEditable(false);
		titulo.setHorizontalAlignment(SwingConstants.CENTER);
		titulo.setText("Alta de Perfil");
		titulo.setBounds(0, 0, 484, 50);
		caja.add(titulo);
		titulo.setColumns(10);
		
		cancelar = new JButton("Cancelar");
		cancelar.setFont(new Font("Eras Medium ITC", Font.PLAIN, 15));
		cancelar.setBounds(341, 242, 116, 24);
		caja.add(cancelar);
		
	/******************************************************************************************************/
		
		//ACCION DEL BOTON CANCELAR
		cancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		
		
		//ACCION DEL BOTON ACEPTAR
		aceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				String nick = campoNick.getText().toUpperCase();
				String email = campoEmail.getText().toUpperCase();
				
				if (!campoEmail.getText().isEmpty() && !campoNick.getText().isEmpty()) {
					ControladorPerfil pp = new ControladorPerfil();
					boolean existeNick = pp.existeNick(nick);
					boolean	existeEmail = pp.existeEmail(email);
				
				    if (existeNick || existeEmail) {
				    	if (existeNick && existeEmail){
				    		JOptionPane.showMessageDialog(null,"EL USUARIO " + nick + " Y EL EMAIL "+ email +" YA EXISTEN");
				    	}
				    	else{
				    		if(existeNick) {
				    			JOptionPane.showMessageDialog(null,"EL USUARIO " + nick + " YA EXISTE");
				    		}
				    		else {
				    			JOptionPane.showMessageDialog(null,"EL EMAIL " + email + " YA EXISTE");
				    		}
				    	}
				    }
					else{
						dispose();
						IAltaPerfil_2 objeto = new IAltaPerfil_2(nick.toUpperCase(), email.toUpperCase());
						objeto.setVisible(true);
						objeto.setResizable(false);
					}
				}
				else{
					JOptionPane.showMessageDialog(null,"Debe ingresar Nick y Email");
				}
			}
		});

	}
	
	//CENTRA LA PANTALLA RPINCIPAL IAltaPerfil
	private void centrarPantalla(JFrame ventana, int x, int y){
		Toolkit t = Toolkit.getDefaultToolkit();
		Dimension pantalla = t.getScreenSize();
		int height = pantalla.height;
		int width = pantalla.width;
		int px =  (int) (width-x)/2;
		int py =  (int) (height-y)/2;
		ventana.setBounds(px, py, x, y);
	}
	
}
